package com.feng.service.impl;

import com.feng.dao.CategoryDao;
import com.feng.dao.ProductDao;
import com.feng.dto.CategoryDto;
import com.feng.dto.ProductDto;
import com.feng.entity.Category;
import com.feng.service.CategoryService;
import com.feng.util.BeanCopyUtil;
import com.feng.util.StringUtil;
import com.feng.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 丰
 * @date: 2022/12/30 14:34
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private ProductDao productDao;

    @Override
    public ResultVo<List<CategoryDto>> getList() {
        List<Category> list = categoryDao.getList();
        List<CategoryDto> categoryDtos = BeanCopyUtil.copyListProperties(list, CategoryDto.class);
        if (categoryDtos != null){
            return ResultVo.success(categoryDtos).code(0);
        }
        return ResultVo.failed();
    }

    @Override
    public ResultVo<List<CategoryDto>> getAllCategory() {
        List<Category> list = categoryDao.getAllCategory();
        List<CategoryDto> categoryDtos = BeanCopyUtil.copyListProperties(list, CategoryDto.class);
        if (categoryDtos != null){
            return ResultVo.success(categoryDtos);
        }
        return ResultVo.failed();
    }

    @Override
    public ResultVo<Integer> add(CategoryDto categoryDto) {
        // 先查询当前元素是否存在
        CategoryDto categoryDto1 = categoryDao.getByName(categoryDto.getName());
        if (categoryDto1 != null){
            return ResultVo.failed();
        }
        String date = StringUtil.getStringDate();
        categoryDto.setCreateTime(date);
        categoryDto.setUpdateTime(date);
        // 调用dao
        int result = categoryDao.add(categoryDto);
        if (result > 0){
            return ResultVo.success(result);
        }
        return ResultVo.failed();
    }

    @Override
    public ResultVo<Integer> deleteById(int id) {
        List<ProductDto> byCategoryId = productDao.findByCategoryId(id);
        // 当前分类不存在产品的引用时，才可以删除
        if (byCategoryId == null || byCategoryId.size() == 0){
            int result = categoryDao.deleteById(id);
            if (result > 0){
                return ResultVo.success(result);
            }
        }
        return ResultVo.failed();
    }

    @Override
    public ResultVo<Integer> deleteAll(Integer[] ids) {
        // 检查删除的元素是否有存在的产品
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            List<ProductDto> byCategoryId = productDao.findByCategoryId(ids[i]);
            // 如果存在产品数据，就添加到集合中
            if (byCategoryId != null && byCategoryId.size() > 0){
                list.add(byCategoryId);
            }
        }
        // 如果集合中不存在数据，则要删除的分类不存在产品
        if (list.size() == 0){
            int result = categoryDao.deleteAll(StringUtil.integersToInts(ids));
            if (result > 0){
                return ResultVo.success(result);
            }
        }
        return ResultVo.failed();
    }

    @Override
    public ResultVo<Category> findById(int id) {
        Category category = categoryDao.findById(id);
        if (category != null){
            return ResultVo.success(category);
        }
        return ResultVo.failed();
    }

    @Override
    public ResultVo<Integer> update(Category category) {
        category.setUpdateTime(StringUtil.getStringDate());
        // 调用dao
        int result = categoryDao.update(category);
        if (result > 0){
            return ResultVo.success(result);
        }
        return ResultVo.failed();
    }
}
